package com.hb.trade.server.project.evaluation.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hb.core.pojo.parent.PageResult;
import com.hb.core.pojo.project.evaluation.dto.EvaluationCommitteeBidDTO;
import com.hb.core.pojo.project.evaluation.entity.EvaluationCommitteeBid;
import com.hb.core.pojo.project.evaluation.mapper.EvaluationCommitteeBidMapper;
import com.hb.trade.server.project.evaluation.service.EvaluationCommitteeBidService;
import lombok.SneakyThrows;
import org.fzzn.core.model.enums.LogicEnum;
import org.fzzn.core.util.Enums;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 评委会标段信息 服务实现类
 * </p>
 *
 * @author ljs
 * @since 2022-1-17
 */
@Service
public class EvaluationCommitteeBidServiceImpl extends ServiceImpl<EvaluationCommitteeBidMapper, EvaluationCommitteeBid> implements EvaluationCommitteeBidService {

    @Override
    @SneakyThrows
    public Integer countEvaluationCommitteeBidBySectionId(Long sectionId) {
        Integer countEvaluationCommitteeBid = this.baseMapper.selectCount(
                new LambdaQueryWrapper<EvaluationCommitteeBid>().eq(EvaluationCommitteeBid::getSectionId, sectionId)).intValue();
        return countEvaluationCommitteeBid;
    }
    /**
     *
     * @Description: 根据评委会id获取 评委会下的标段信息
     * @Author: zhongdl
     * @Date:2022/2/15 11:39
     * @param evaluationCommitteeBid:
     * @return: {@link PageResult< EvaluationCommitteeBidDTO> }
     **/
    @Override
    public PageResult<EvaluationCommitteeBidDTO> pageCommitteeBidBid(EvaluationCommitteeBidDTO evaluationCommitteeBid) {
        Page<EvaluationCommitteeBidDTO> pages = evaluationCommitteeBid.pages();
        pages = baseMapper.pageCommitteeBidBid(pages,evaluationCommitteeBid.getEvaluationCommitteeId());
        return PageResult.get(pages);
    }
    /**
     *
     * @Description: 结束评标
     * @Author: zhongdl
     * @Date:2022/2/15 16:54
     * @param evaluationCommitteeId:评委会id
     * @param sectionId: 标段id
     * @return: {@link boolean }
     **/
    @Override
    public boolean endEvaluation(Long evaluationCommitteeId, Long sectionId) {
        UpdateWrapper<EvaluationCommitteeBid> wrapper = new UpdateWrapper<>();
        wrapper.set("end_flag", LogicEnum.YES.getCode())
                .eq("evaluation_committee_id",evaluationCommitteeId)
                .eq("section_id",sectionId);
        return update(wrapper);
    }
    /**
     *
     * @Description:判断下评委会标段是否全部结束评标
     * @Author: zhongdl
     * @Date:2022/2/15 17:01
     * @param evaluationCommitteeId:评委会id
     * @return: {@link boolean }
     **/
    @Override
    public boolean endAllSectionEvaluationFlag(Long evaluationCommitteeId) {
        QueryWrapper<EvaluationCommitteeBid> wrapper = EvaluationCommitteeBid.wrapper();
        wrapper.eq("evaluation_committee_id",evaluationCommitteeId);
        List<EvaluationCommitteeBid> list = list(wrapper);
        if (CollUtil.isEmpty(list)){
            return false;
        }
        //已经结束评标的标段
        int size = list.stream().filter(evaluationCommitteeBid -> Enums.equals(LogicEnum.YES, evaluationCommitteeBid.getEndFlag())).collect(Collectors.toList()).size();
        return list.size() == size;
    }

}
